package surreal.triviamaster.gui;

import surreal.triviamaster.R;
import surreal.triviamaster.utilities.TriviaMasterApplication;
import surreal.triviamaster.utilities.Utilities;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AddQuestion extends ListActivity{
		private String aAs;
		private String question;
	@Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	    setContentView(R.layout.add_question);
	    
		 // getting the data that was passed to this activity
		Bundle bundle = getIntent().getExtras();
		Bundle qBundle = bundle.getBundle("bundle");
    	question = qBundle.getString("question");
    	aAs=qBundle.getString("aAs");
	    
	    //updating the gui
	    ((TextView) findViewById(R.id.question)).setText(question);
	    setListActivity();
	    
	    //Add Answers
	    Button addAnswer = (Button) findViewById(R.id.addAnswer);
	    addAnswer.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	String a=((TextView) findViewById(R.id.answer)).getText().toString();
            	if(a.equals("")||Utilities.isValidToUpload(a)==false) {
            		Toast.makeText(AddQuestion.this, TriviaMasterApplication.ERROR_INVALID_CHARACTERS, 10000).show();
            		
            	} else if(aAs.equals("")) {
            		//if the aAs is empty just add the answer
            		aAs=a;
            		((TextView) findViewById(R.id.answer)).setText("");
            	} else {
            		//else add a delimiter before the answer
            		aAs+=";"+a;
            		((TextView) findViewById(R.id.answer)).setText("");
            	}
            	setListActivity();
            	//((TextView) findViewById(R.id.acceptableAnswers)).setText("All Answers: " +aAs);
            }
        });
	    
	    //Next Page button
	    Button nextPage = (Button) findViewById(R.id.nextPage);
	    nextPage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	String q=((TextView) findViewById(R.id.question)).getText().toString();
            	if(aAs.equals("")||q.equals("")){
            		//if user has not supplied any answers
            		Toast.makeText(AddQuestion.this, "You must add at least one Answer and a Question", 10000).show();
            		
            	} else if (Utilities.isValidToUpload(q)==false){
            		Toast.makeText(AddQuestion.this, TriviaMasterApplication.ERROR_INVALID_CHARACTERS, 10000).show();
            	}else {
            		
            		//passing question data to the next page
            		Bundle bundle = new Bundle();
            		bundle.putString("question", ((TextView) findViewById(R.id.question)).getText().toString());
            		bundle.putString("aAs", Utilities.analyzeaAs(aAs));
            		bundle.putString("fList", "");
            		bundle.putString("hint", "");
            		
            		Intent intent = new Intent(AddQuestion.this, AddHints.class);
            		intent.putExtra("bundle", bundle);
	  	      		startActivity(intent);
            	}
            }
        });
	    
	    //Cancel button -> Home
	    Button cancel = (Button) findViewById(R.id.cancel);
	    cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(AddQuestion.this, TriviaMasterActivity.class);
	  	      	startActivity(intent);
            }
        });
        
	}
	
	private void setListActivity() {
		
    	String[] aList=aAs.split(";");
    	setListAdapter(new ArrayAdapter<String>(this, R.layout.generic_list_item, aList));
		
	}
}
